package com.charon.auth.demo.exception.handler;

import com.charon.auth.demo.exception.UserNotExistException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.Map;

/**
  *@ClassName: ControllerExceptionHandler
  *@Description: 统一异常处理
  *@Author: Charon [248135724@qq.com]
  *@Date: 2019/1/12 0012 23:35
  *@Version: 1.0
  */
@Slf4j
@ControllerAdvice
public class ControllerExceptionHandler {

    //告诉方法处理什么异常
    @ExceptionHandler(UserNotExistException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String,Object> handlerUserNotExistException(UserNotExistException unee){
        log.error("出现异常："+unee.getMessage());
        HashMap<String,Object> hash=new HashMap<String, Object>();
        hash.put("id",unee.getId());
        hash.put("message",unee.getMessage());
        return hash;
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String,Object> handlerException(Exception exception){
        log.error("异常处理："+exception.toString());
        HashMap<String,Object> hash=new HashMap<String, Object>();
        hash.put("ClassName：",exception.getClass().getName());
        hash.put("Message",exception.getMessage());
        return hash;
    }

}
